package com.zx.scp.controller;

import javax.servlet.http.HttpServletRequest;

import com.zx.map.utils.CommonResponseUtils;
import com.zx.map.utils.SUConstants;
import com.zx.scp.service.IIncomeService;
import com.zx.scp.service.model.*;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 扫码收款
 */
@Controller
@RequestMapping("/income/kubei")
public class KubeiIncomeController {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(KubeiIncomeController.class);

    @Autowired
    @Qualifier("kubeiIncomeServiceImpl")
    private IIncomeService incomeService;

    @RequestMapping("/nofityPay")
    public String notifyOrder(@RequestParam Map map, Model model,
                              HttpServletRequest request) {
        try {
            log.debug("收款通知："+ ToStringBuilder.reflectionToString(map));
            Map resultMap=incomeService.payNotify(map);
            return (String) resultMap.get(SUConstants.MSG_KEY);
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    @RequestMapping("/startPay")
    @ResponseBody
    public Map getPay(HttpServletRequest httpServletRequest,YintongPayRequestModel payRequestModel, Model model) {
        try {
            return incomeService.startPay(httpServletRequest,payRequestModel);
        } catch (Exception e) {
            log.error("获取二维码异常", e);
            return CommonResponseUtils.failureResponse(SUConstants.SERVER_ERROR);
        }
    }

    /**
     * 微信公众号支付
     * @param orderResponseDomain
     * @param request
     * @return
     */
    @RequestMapping("/winxinJsPay")
    @ResponseBody
    public Map winxinJsPay(YintongPayRequestModel orderResponseDomain,HttpServletRequest request){
        try {
            return  incomeService.winxinJsPay(request,orderResponseDomain);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取二维码异常", e);
            return CommonResponseUtils.failureResponse(SUConstants.SERVER_ERROR);
        }
    }
}
